package com.qf.demo;

import com.qf.util.ZkUtil;
import org.apache.curator.framework.CuratorFramework;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;
import org.junit.Test;

import java.util.List;

/**
 * @author jbx
 * @package_name com.qf.demo
 * @create_date 2020/10/27 19:13
 */
public class Demo2 {
    CuratorFramework cf = ZkUtil.cf();
    //获取子节点
    @Test
    public void getChildren() throws Exception {
        List<String> strings = cf.getChildren().forPath("/");

        for (String string : strings) {
            System.out.println(string);
        }
    }

    // 获取节点数据
    @Test
    public void getData() throws Exception {
        byte[] bytes = cf.getData().forPath("/qf");
        System.out.println(new String(bytes,"UTF-8"));
    }
    //创建结点以及数据
    @Test
    public void create() throws Exception {
        cf.create().withMode(CreateMode.PERSISTENT).forPath("/qf","uuuu".getBytes());
    }

    //修改结点的数据
    @Test
    public void update() throws Exception {
        cf.setData().forPath("/qf","zzzz".getBytes());
    }
    //删除结点
    @Test
    public void delete() throws Exception {
        cf.delete().deletingChildrenIfNeeded().forPath("/qf");
    }
    //查看结点状态
    @Test
    public void stat() throws Exception {
        Stat stat = cf.checkExists().forPath("/qf");
        System.out.println(stat);
    }
}
